<?php

/*
 * This file belongs to Techno Web Solutions :: http://technowebsol.com
 */

/**
 * Description of MySQL
 * Sep 13, 2012
 * File: mysql.php
 * Encoding: UTF-8
 * Project: fbClone
 *
 * @author Suman Choudhury <suman@technowebsol.com>
 */
class MySQl{
	private $host;
	private $user;
	private $pass;
	private $port;
	private $conn = NULL;
    private $link = NULL;
    private $dbName;
    public $error;
    public $last_query;
    private $query;
    private $numRows;
    private $result = array();
    private $row;
    public $dbPrefix = "fb_";
    function __construct($host="", $user="", $pass="", $dbName="", $port="", $dbPrefix=""){
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->port = $port;
        $this->dbName = $dbName;
        $this->dbPrefix = $dbPrefix;
        if($this->host!="" && $this->user!="" && $this->pass!="" && $this->dbName!=""){
            $this->connect();
        }
	}

    function get($table, $field="*", $cond=array()){
        $sql = "SELECT $field FROM ".$this->dbPrefix.$table." ";
        $condSql = "";
        $sep = " ";
        if(!empty($cond)){
            foreach($cond AS $field=>$val){
                $condSql .= $sep.$field."='".$val."' ";
                $sep = " AND ";
            }
            $sql .= " WHERE ".$condSql;
        }
        $query = $this->execQuery($sql);
        $this->setNumRows($query);
        //echo $this->getNumRows();
        return $this->getRows();
    }

    function execQuery($sql){
        $this->query = $this->runQuery($sql);
    }
    function query($sql){
        $this->query = $this->runQuery($sql);
        $this->setNumRows();
    }
    function getRows($sql=""){
        if($sql!="")$this->query = $this->runQuery($sql);
        //else
        $this->setNumRows();
        if($this->numRows > 0){
            while ($row = mysql_fetch_object($this->query)) {
                $this->result[] = $row;
            }
            return $this->result;
        }
        return FALSE;
    }
    function getRow($sql=""){
        if($sql!="")$this->query = $this->runQuery($sql);
        $this->setNumRows();
        if($this->numRows > 0){
            $this->row = mysql_fetch_object($this->query);
            return $this->row;
        }
        return FALSE;
    }
    function getNumRows(){
        return $this->numRows;
    }
    function setNumRows(){
        $this->numRows = mysql_num_rows($this->query);
    }

    function delete($table="", $cond=array()){
        $condSql = "";
        $sep = " ";
        if(!empty($cond)){
            foreach($cond AS $field=>$val){
                $condSql .= $sep.$field."='".$val."' ";
                $sep = " AND ";
            }
        }else{
            $this->error = "Need primary key condition";
            return FALSE;
        }
        $sql = "DELETE FROM $this->dbPrefix.$table WHERE $condSql";
        return $this->runQuery($sql);
    }
    function insert($table="", $data = array()){
        if(!empty($data)){
            $strData = "";
            $sep = "";
            foreach($data AS $field=>$val){
                $strData .= $sep.$field."='".mysql_real_escape_string($val)."'";
                $sep = ",";
            }//array("username"=>"jeno",);
            $sql = "INSERT INTO $this->dbPrefix.$table SET $strData";
            if($this->runQuery($sql)){
                return $this->getLastInsertId();
            }
            $this->error = "Failed to insert into $table. SQL:- $sql";
            return FALSE;
        }
    }
    function update($table="", $data= array(), $cond=array()){
        if(!empty($data)){
            $strData = "";
            $sep = "";
            foreach($data AS $field=>$val){
                $strData .= $sep.$field."='".$val."'";
                $sep = ",";
            }
            $condSql = "";
            $sep = " ";
            if(!empty($cond)){
                foreach($cond AS $field=>$val){
                    $condSql .= $sep.$field."='".mysql_real_escape_string($val)."' ";
                    $sep = " AND ";
                }
            }else{
                $this->error = "Need primary key condition";
                return FALSE;
            }
            $sql = "UPDATE $this->dbPrefix.$table SET $strData";
            $sql .= " WHERE $condSql";
            if($this->runQuery($sql)){
                return TRUE;
            }
            $this->error = "Failed to update $this->dbPrefix.$table. SQL:- $sql";
            return FALSE;
        }
    }

    function runQuery($sql = ""){
        $result = mysql_query($sql);
        return $result;
    }

    function getLastInsertId(){
        return mysql_insert_id();
    }

    function connect(){
        if($this->host!="" && $this->user!="" && $this->pass!="" && $this->dbName!=""){
            $server = $this->host;
            if($this->port){
                $server .= ":".$this->port;
            }
            $this->link = mysql_connect($server, $this->user, $this->pass);
            if($this->link === FALSE){
                $this->error = "Unable to connect to the database on $this->host with username $this->user";
                return FALSE;
            }
            $this->conn = mysql_select_db($this->dbName, $this->link);
            if($this->conn === FALSE){
                $this->error = "Unable to select $this->dbName";
                return FALSE;
            }
            return $this->conn;
        }
        else{
            $this->error = "Please provide database login information";
            return FALSE;
        }
    }
    function close(){
        mysql_close();
    }
    function isConnected(){

    }
    function getDbName(){
        return $this->dbName;
    }
    function setDbName($dbName){
        $this->dbName = $dbName;
    }
    function getHost(){
        return $this->host;
    }
    function setHost($host){
        $this->host = $host;
    }

    function getUser(){
        return $this->user;
    }
    function setUser($user){
        $this->user = $user;
    }
    function getPass(){
        return $this->pass;
    }
    function setPass($pass){
        $this->pass = $pass;
    }
    function getPort(){
        return $this->port;
    }
    function setPort($port){
        $this->port = $port;
    }
}

?>
